/*
 * Task functions
 */
function makeTaskItemHeader(id,name){
	var prefix='task_item_header_',classes=['ui-widget-header','task_item_header'],jObj=$("<div/>");
	jObj.addClass(classes.join(' '));
	jObj.text(name).dblclick(function(event){
		
	});
	return jObj
}
function makeTaskItemContent(task_id){
	var prefix='task_item_content_',classes=['task_item_content'],jObj=$("<div/>");
	jObj.attr('id',prefix+task_id).addClass(classes.join(' ')).append(initDetailItem(task_id)).append(initAddDetailButton());
	return jObj;
}
function makeTaskItemFooter(task_id){
	var prefix='task_item_footer_',classes=['task_item_footer','ui-resizable-handle','ui-resizable-s'],jObj=$("<div/>");
	jObj.attr('id',prefix+task_id).addClass(classes.join(' ')).text('Tags:');
	$.ajax({
		type: 'GET',
		url: 'ajax/ajax.php',
		data: {action:'query_tags',id:task_id,rnd:Math.random()},
		success: function(result){
			if (result!='false'){	
				jObj.text(jObj.text()+result);
			}
			
		}
	});
	return jObj;
}
function makeTaskItem(id,name){
	var prefix='task_item_',classes=['task_item','ui-widget-content','ui-corner-all'],jObj=$("<div/>");
	jObj.addClass(classes.join(' '));
	jObj.attr('id',prefix+id)
		.resizable({
				stop: updateTaskItemSize,
				autoHide: true,
				ghost: true,
				minHeight: 200,
				minWidth: 300,
				maxHeight: 400,
				maxWidth: 400
		})
		.draggable({
			containment : "parent",
			stack: ".task_item",
			stop : updateTaskItemPos
		});
	jObj.append(makeTaskItemHeader(id,name));
	jObj.append(makeTaskItemContent(id));
	jObj.append(makeTaskItemFooter(id));
	return jObj;
}


function initTaskTabContext(){
	$(".task_tab").jeegoocontext('task_tab_context',{
		onSelect: function(event,obj){
			var task_tab =$(obj);
			switch ($(this).attr('id')){
				case 'new_task': 				
					var input_box=$("<input id='new_task_name'/>");
					input_box.addClass('ui-widget-contentt ui-corner-all');
					$("#task_item_dialog").text("輸入任務名稱？").append(input_box).dialog({
						title: "新增任務",
						show: "highlight",
						buttons:{
							'確定': function(){
								var task_name = $('#new_task_name').val();
								addTaskItem(task_tab,task_name);
								$(this).dialog('close');
							},
							'取消': function(){ 
								$(this).dialog('close');
							}
						}
					});
					break;
			}
		}
	});
}
//Initial task-item from DB
function initTaskItem(list_id){
	var jObj=$("<div/>"),prefix='task_';
	jObj.attr('id',prefix+list_id);
	jObj.addClass('task_tab');
	$.ajax({
		type: 'GET',
		url: 'ajax/ajax.php',
		data: {action:'tasks',id:list_id,rnd:Math.random()},
		dataType: 'json',
		success: function(result){
			//alert(result);
			$.each(result,function(i){
				var pos=result[i].position.split(',');
				var size=result[i].size.split(',');
				var id=result[i].id,name=result[i].name,task_item=makeTaskItem(id,name);
				task_item.css('top',pos[0]);
				task_item.css('left',pos[1]);
				task_item.css('width',size[0]);
				task_item.css('height',size[1]);
				task_item.removeData('created').data('created',new Date(result[i].created_date*1000));
				jObj.append(task_item);
			});
			initTaskItemContext();
			initTaskTabContext();
		}
		// end ajax success function
	});
	$("#lists_tab").append(jObj);
}
function initTaskItemContext(){
	$(".task_item").jeegoocontext('task_item_context',{
		onShow: function(event,obj){
			var task_item=$(obj),isDock=task_item.draggable('option','disabled');
			if (isDock){
				$("#dock_task").text('解鎖');
			}else{
				$('#dock_task').text('鎖定');
			}
			$('#task_item_context #created').text('建立日期:'+$(obj).data('created'));
		},
		onSelect: function(event,obj){
			var task_item=$(obj);
			switch($(this).attr('id')){
				case 'dock_task':
					var isDock=task_item.draggable('option','disabled');
					var options=isDock?'enable':'disable';
					task_item.draggable(options);
					break;
				case 'del_task':
					$('#task_item_dialog').text('是否要刪除此任務？').dialog({
						title: '刪除任務',
						show: 'highlight',
						buttons: {
							'確定': function(){
								var task_id = task_item.attr('id');
								delTaskItem(task_item,task_id);
								$(this).dialog('close');
							},
							'取消': function(){
								$(this).dialog('close');
							}
						}
					});			
					break;
				case 'ren_task':
					var input_box = $("<input id='rename_task_name'/>");
					input_box.addClass('ui-widget-content ui-corner-all');
					$('#task_item_dialog').text('輸入欲要更改的任務名稱？').append(input_box).dialog({
						title: "重新命名任務",
						buttons: {
							"確定": function(){
								var task_id = task_item.attr('id');
								var task_name = $("#rename_task_name").val();
								renTaskItem(task_item,task_id,task_name);
								$(this).dialog('close');
							},
							"取消": function(){
								$(this).dialog('close');
							}
							
						}
					});
					break;
			}
		}
	});
}
function reloadTaskItem(task_tab,list_id){
	task_tab.remove();
	initTaskItem(list_id);	
}
function addTaskItem(task_tab,task_name){
	var list_id = task_tab.attr('id').split('_').pop();
	$.ajax({
		type: 'GET',
		url: 'ajax/ajax.php',
		data: {action:'new_task',name:task_name,id:list_id,rnd:Math.random()},
		success: function(result){
			//alert(result);
			if (result!='false'){
				reloadTaskItem(task_tab,list_id);
			}
		}
		// end ajax success function
	});
}
function delTaskItem(task_item,task_id){
	$.ajax({
		type: 'GET',
		url: 'ajax/ajax.php',
		data: {action:'del_task',id:task_id,rnd:Math.random()},
		success: function(result){
			//alert(result);
			if (result=='true')
				task_item.remove();
		}
	});
}
function renTaskItem(task_item,task_id,task_name){
	$.ajax({
		type: 'GET',
		url: 'ajax/ajax.php',
		data: {action:'ren_task',id:task_id,name:task_name,rnd:Math.random()},
		success: function(result){
			if (result == 'true'){
				//alert(result);
				task_item.find('.task_item_header').text(task_name);
			}
		}
		//end ajax success function
	});
}
function updateTaskItemPos(event,ui){
	var position=String.format("{1}px,{2}px",ui.position.top,ui.position.left),task_item_id=$(this).attr('id');
	$.ajax({
		type: 'GET',
		url: 'ajax/ajax.php',
		data: {action:'repos',id:task_item_id,pos:position,rnd:Math.random()},
		success: function(result){
			//result boolean
			//alert(result);
			//ui.helper.offset({top:ui.position.top,left:ui.position.left});
		}
		//end ajax success function
	});
}
function updateTaskItemSize(event,ui){
	var resize=String.format("{1}px,{2}px",ui.size.width,ui.size.height),task_item_id=$(this).attr('id');
	$.ajax({
		typd: 'GET',
		url: 'ajax/ajax.php',
		data: {action:'resize',id:task_item_id,size:resize,rnd:Math.random()},
		success: function(result){
			//return boolean
			//alert(result);
		}
		//end ajax success function
	});
}
